//
//  XS_CertSubVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/11.
//

import UIKit

enum XS_CertSubVCType {
    case platform
    case qualifications
}

class XS_CertSubVC: XS_BaseVC {
    
    var type : XS_CertSubVCType = .platform
    var dataArray : [XS_CertItemModel] = []
    
    var nav : UINavigationController?

    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib.init(nibName: "XS_CertItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_CertItemCell")
        collectionView.register(XD_PingTaiCertItemCell.self, forCellWithReuseIdentifier: "XD_PingTaiCertItemCell")
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        if type == .qualifications {
            collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(requestData))
            requestData()
        }
    }

    @objc func requestData(){
        var api = "/userCertification/queryAppCer"
        if type == .qualifications {
            api = "/userCertification/queryUserCer"
        }
        XS_NetWork.requestJson(api)
            .mapModels(type: XS_CertItemModel.self) // 返回 模型数组 和原始数据
            .subscribe(onNext: { array, resp in
                if array.count > 0 {
                    XD_BlankPageView.hiddenBlankPageView(superView: self.collectionView)
                    self.dataArray = array
                    self.collectionView.reloadData()
                } else {
                    if self.type == .platform {
                        XD_BlankPageView.blankPage(superView: self.collectionView, imgName: "缺省页_暂无内容", detailStr: "暂无内容~", bottomDetailStr: "学习证书课程，即可获取平台证书")
                    } else {
                        XD_BlankPageView.blankPage(superView: self.collectionView, imgName: "缺省页_暂无内容", detailStr: "暂无内容~", btnTitle: "上传我的证书", bottomDetailStr: "") {
                            let uploadVC = XS_CertUploadVC()
                            self.nav?.pushViewController(uploadVC, animated: true)
                        }
                    }
                }
                self.collectionView.mj_header?.endRefreshing()
            }).disposed(by: disposeBag)
    }
}

extension XS_CertSubVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if type == .qualifications {
            return dataArray.count
        } else {
            return 3
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if type == .qualifications {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_CertItemCell", for: indexPath) as! XS_CertItemCell
            if indexPath.item < dataArray.count {
                cell.model = dataArray[indexPath.item]
            }
            return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XD_PingTaiCertItemCell", for: indexPath) as! XD_PingTaiCertItemCell
            cell.imgView.image = UIImage.init(named: ["chuji", "zhongji", "gaoji"][indexPath.row])
            cell.nameLabel.text = ["初级学者", "中级学者", "高级学者"][indexPath.row]
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if type == .qualifications {
            return CGSize.init(width: floor((.screenWidth - 60) / 3), height: 200)
        } else {
            return CGSize.init(width: floor((.screenWidth - 60) / 3), height: floor((.screenWidth - 60) / 3) + 24)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 15, left: 15, bottom: 15, right: 15)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if type == .qualifications {
            let model = dataArray[indexPath.item]
            let vc = XS_PingtaiZhengshuDetailVC()
            vc.id = Int(model.id)
            nav?.pushViewController(vc, animated: true)
        }
    }
}
